import React, { Component } from 'react';
import '../css/Information.less';
import {observer} from "mobx-react";
import  Mobx from  '../mobx'
import  axios from  'axios'
import InformationSon from  './InformationSon'
@observer class Information extends Component {
    constructor(props) {
        super(props)
        this.state={
            Zixun:[],
            total:'',
            size:''
        }
    }
    componentWillMount(){
        Mobx.history= this.props.history
    }
    componentDidMount(){
        // 获取资讯
        axios.get('http://www.niuduxiu.com/pc/article/page/%E5%B0%8F%E7%89%9B%E5%A4%B4%E6%9D%A1/%E5%A4%B4%E6%9D%A1%E8%B5%84%E8%AE%AF.jhtml?pageNumber=1&pageSize=8')
            .then((item)=>{
                this.setState({
                    Zixun:item.data.data.content,
                    total:item.data.data.totalPages,
                    size:item.data.data.pageNumber
                },()=>{
                    this.interval();
                })
            })
    }
    interval(){
        var interval1=setInterval(()=>{
            if((document.documentElement.scrollTop||document.body.scrollTop)-document.body.scrollHeight>=-1000){
                clearInterval(interval1)
                this.setState({
                    size:this.state.size+1
                },()=>{
                    this.loading()
                })
            }
        },1000)
    }
    loading(){
        if(this.state.total<this.state.size){
            return false;
        }
       if(this.state.size==1){
           axios.get('http://www.niuduxiu.com/pc/article/page/%E5%B0%8F%E7%89%9B%E5%A4%B4%E6%9D%A1/%E5%A4%B4%E6%9D%A1%E8%B5%84%E8%AE%AF.jhtml?pageNumber='+this.state.size+'&pageSize=8')
               .then((item)=>{
                   this.setState({
                       Zixun:item.data.data.content,
                       total:item.data.data.totalPages,
                       size:item.data.data.pageNumber
                   },()=>{
                       if(this.state.total>this.state.size){
                           return false;
                       }
                       this.interval();
                   })
               })
       }else{
           axios.get('http://www.niuduxiu.com/pc/article/page/%E5%B0%8F%E7%89%9B%E5%A4%B4%E6%9D%A1/%E5%A4%B4%E6%9D%A1%E8%B5%84%E8%AE%AF.jhtml?pageNumber='+this.state.size+'&pageSize=8')
               .then((item)=>{
                   this.setState({
                       Zixun:this.state.Zixun.concat(item.data.data.content),
                       total:item.data.data.totalPages,
                       size:item.data.data.pageNumber
                   },()=>{
                       if(this.state.total>this.state.size){
                           return false;
                       }
                       this.interval();
                   })
               })
       }
    }
    render() {
        return (
            <div className='informationParents'>
                <p className='titleName'>资讯</p>
                <InformationSon Zixun={this.state.Zixun} />
                <div className='ApptitleName' style={{'display':this.state.total<=this.state.size?"-webkit-flex":"none"}}>暂无更多了。。</div>
                <div className='ApptitleName' style={{'display':this.state.total>this.state.size?"-webkit-flex":"none"}}>正在加载中。。</div>
            </div>
        );
    }
}

export default Information;
